/*
 *   项目名称：train12306
 *   文件名称：com.xiaolon.train12306.member.service.PassengerService
 *
 *   创建人：  ZHOU XIAO LONG
 *   创建日期：2024/12/24
 *
 */
package com.xiaolon.train12306.member.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.log.Log;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xiaolon.train12306.common.context.LoginMemberContext;
import com.xiaolon.train12306.common.req.PageReq;
import com.xiaolon.train12306.common.resp.PageResp;
import com.xiaolon.train12306.common.util.SnowUtil;
import com.xiaolon.train12306.member.domain.Passenger;
import com.xiaolon.train12306.member.domain.PassengerExample;
import com.xiaolon.train12306.member.mapper.PassengerMapper;
import com.xiaolon.train12306.member.req.PassengerQueryReq;
import com.xiaolon.train12306.member.req.PassengerSaveReq;
import com.xiaolon.train12306.member.resp.PassengerQueryResp;
import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 类描述： PassengerService
 **/


@Service
public class PassengerService {

    // 日志
    private static final Logger LOG = LoggerFactory.getLogger(PassengerService.class);

    @Resource
    private PassengerMapper passengerMapper;

    /**
     * 保存乘客信息
     * @param req
     */
    public void save(PassengerSaveReq req){
        DateTime now = DateTime.now();
        Passenger passenger = BeanUtil.copyProperties(req, Passenger.class);

        // 不存在id，则新增；否则，更新信息；
        if(ObjectUtil.isNull(passenger.getId())){
            // 设值登录会员id, 需要从上下文中获取
            passenger.setMemberId(LoginMemberContext.getId());

            passenger.setId(SnowUtil.getSnowflakeNextId());
            passenger.setCreateTime(now);
            passenger.setUpdateTime(now);
            passengerMapper.insert(passenger);
        }else{
            passenger.setUpdateTime(now);
            passengerMapper.updateByPrimaryKey(passenger);
        }
    }

    /**
     * 查询乘客信息
     * @param req
     * @return
     */
    public PageResp<PassengerQueryResp> queryList(PassengerQueryReq req){
        PassengerExample passengerExample = new PassengerExample();
        passengerExample.setOrderByClause("id desc");
        PassengerExample.Criteria criteria = passengerExample.createCriteria();

        if(ObjectUtil.isNotNull(req.getMemberId())){
            criteria.andMemberIdEqualTo(req.getMemberId());
        }

        LOG.info("查询页码: {}", req.getPage());
        LOG.info("每页条数: {}", req.getSize());

        PageHelper.startPage(req.getPage(), req.getSize());
        List<Passenger> passengerList = passengerMapper.selectByExample(passengerExample);

        PageInfo<Passenger> pageInfo = new PageInfo<>(passengerList);
        LOG.info("总行数: {}", pageInfo.getTotal());
        LOG.info("总页数: {}", pageInfo.getPages());

        List<PassengerQueryResp> list = BeanUtil.copyToList(passengerList, PassengerQueryResp.class);

        PageResp<PassengerQueryResp> pageResp = new PageResp<>();
        pageResp.setTotal(pageInfo.getTotal());
        pageResp.setList(list);
        return pageResp;
    }

    /**
     * 查询乘客信息
     * @param id
     */
    public void delete(Long id){
        passengerMapper.deleteByPrimaryKey(id);
    }

    /**
     * 查询我的所有乘客
     * @return
     */
    public List<PassengerQueryResp> queryMine(){
        PassengerExample passengerExample = new PassengerExample();
        passengerExample.setOrderByClause("name asc");
        PassengerExample.Criteria criteria = passengerExample.createCriteria();
        criteria.andMemberIdEqualTo(LoginMemberContext.getId());
        List<Passenger> list = passengerMapper.selectByExample(passengerExample);
        return BeanUtil.copyToList(list, PassengerQueryResp.class);
    }
}